#include "MessageBox.h"
#include "hotkeyedit.h"
#include "syssettingform.h"
#include "ui_syssettingform.h"

#include <QLineEdit>
#include <QScroller>
#include <QScrollerProperties>
#include <mainwindow.h>

#include "models/syssettingmodel.h"
#include"models/command.h"

#include <sitckcontrol/stickcontrol.h>

SysSettingForm::SysSettingForm(QWidget *parent)
    : QWidget(parent)
{
    initUI();
}

void SysSettingForm::initUI()
{
    // 设置背景颜色和全局字体
    setStyleSheet(
        "QWidget {"
        "    background-color: #1F1F1F;"
        "    font-family: '微软雅黑';"
        "}"
        );
    // 创建并设置标签页
    m_tabWidget = new QTabWidget(this);
    m_tabWidget->setDocumentMode(true);
    m_tabWidget->tabBar()->setVisible(false);
    m_tabWidget->setStyleSheet("QTabWidget::pane { border: none; }");

    // // 创建页面
    // m_videoWidget = new VideoWidget(m_tabWidget);
    m_pGlobalWidget = new GlobalSettingWidget(m_tabWidget);
    m_pUpperWidget = new UpperSettingWidget(m_tabWidget);
    m_pAudioWidget = new AudioFileWidget(m_tabWidget);
    m_pOtherWidget = new OtherWidget(m_tabWidget);

    m_tabWidget->addTab(m_pGlobalWidget, "全局动作");
    m_tabWidget->addTab(m_pUpperWidget, "上肢动作");
    m_tabWidget->addTab(m_pAudioWidget, "音频文件");
    m_tabWidget->addTab(m_pOtherWidget, "其他设置");

    // 创建头部区域
    QWidget *headerWidget = new QWidget(this);
    headerWidget->setFixedHeight(60*g_xScale);
    QHBoxLayout *titleLayout = new QHBoxLayout(headerWidget);
    titleLayout->setContentsMargins(28*g_xScale, 0, 28*g_xScale, 0);

    // 创建标题标签
    m_pGlobalBtn = new TouchButton("全局动作",this);
    m_pGlobalBtn->setCheckable(true);
    m_pGlobalBtn->setChecked(true);
    m_pUpperBtn = new TouchButton("上肢动作",this);
    m_pUpperBtn->setCheckable(true);
    m_pAudioBtn = new TouchButton("音频文件",this);
    m_pAudioBtn->setCheckable(true);
    m_pOtherBtn = new TouchButton("其它设置",this);
    m_pOtherBtn->setCheckable(true);
    QString style=  QString("QPushButton {"
                            "    background-color: #00000000;"
                            "    color: white ;"
                            "    border: none;"
                            "    border-radius: 4px;"
                            "    font:%1px '微软雅黑';"
                            "}"
                            "QPushButton:checked{"
                            "    color: #0056b3 ;"
                            "}").arg(int(20*g_xScale));
    m_pGlobalBtn->setStyleSheet(style);
    m_pUpperBtn->setStyleSheet(style);
    m_pAudioBtn->setStyleSheet(style);
    m_pOtherBtn->setStyleSheet(style);

    m_pButtonGroup = new QButtonGroup(this);
    m_pButtonGroup->setExclusive(true);
    m_pButtonGroup->addButton(m_pGlobalBtn,0);
    m_pButtonGroup->addButton(m_pUpperBtn,1);
    m_pButtonGroup->addButton(m_pAudioBtn,2);
    m_pButtonGroup->addButton(m_pOtherBtn,3);
    connect(m_pButtonGroup, SIGNAL(buttonToggled(int,bool)), this, SLOT(onSwitchPage(int,bool)));

    titleLayout->addWidget(m_pGlobalBtn);
    QLabel *label = new QLabel("|",this);
    label->setStyleSheet(
        QString(
            "color: #48494E;"
            "    font:%1px '微软雅黑';"
            ).arg(int(20*g_xScale)));
    label->setFixedWidth(5*g_xScale);  // 改为最小宽度
    label->setAlignment(Qt::AlignCenter);
    titleLayout->addWidget(label);
    titleLayout->addWidget(m_pUpperBtn);
    label = new QLabel("|",this);
    label->setStyleSheet(
        QString(
            "color: #48494E;"
            "    font:%1px '微软雅黑';"
            ).arg(int(20*g_xScale)));
    label->setFixedWidth(5*g_xScale);  // 改为最小宽度
    label->setAlignment(Qt::AlignCenter);
    titleLayout->addWidget(label);
    titleLayout->addWidget(m_pAudioBtn);

    label = new QLabel("|",this);
    label->setStyleSheet(
        QString(
            "color: #48494E;"
            "    font:%1px '微软雅黑';"
            ).arg(int(20*g_xScale)));
    label->setFixedWidth(5*g_xScale);  // 改为最小宽度
    label->setAlignment(Qt::AlignCenter);
    titleLayout->addWidget(label);
    titleLayout->addWidget(m_pOtherBtn);
    titleLayout->addStretch();
}

void SysSettingForm::resizeEvent(QResizeEvent *event)
{
    QWidget::resizeEvent(event);
    m_tabWidget->setGeometry(0,0,width(),height());
}

void SysSettingForm::onSwitchPage(int nID, bool b)
{
    if(b) {
        m_tabWidget->setCurrentIndex(nID);
    }
}

void SysSettingForm::hideEvent(QHideEvent *event)
{
    Q_UNUSED(event)
    m_pGlobalBtn->setChecked(true);
}
